﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class AccessorialChargesBLL
    {
        private static readonly IAccessorialChargesDA AccessorialChargesDA = DataAccess.CreateAccessorialChargesDA();
        private static readonly IAccessorialDA accessorialDA = DataAccess.CreateAccessorialDA();
        private static readonly IBillingCredenceDA billingCredenceDA = DataAccess.CreateBillingCredenceDA();
        private static readonly IBillingEntityDA billingEntityDA = DataAccess.CreateBillingEntityDA();
        private static readonly ICarrierDA carrierDA = DataAccess.CreateCarrierDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly ICustomerDA customerDA = DataAccess.CreateCustomerDA();
        private List<string> dataBaseID;
        private static readonly IGeneralLedgerDA generalLedgerDA = DataAccess.CreateGeneralLedgerDA();
        private static readonly IMovementDA movementDA = DataAccess.CreateMovementDA();
        private static readonly IOrdersDA ordersDA = DataAccess.CreateOrdersDA();
        private PubParasInfo pubParasInfo;
        private static readonly IReceiptDA receiptDA = DataAccess.CreateReceiptDA();
        private static readonly ITradePartnerDA tradePartnerDA = DataAccess.CreateTradePartnerDA();
        private static readonly IVendorDA vendorDA = DataAccess.CreateVendorDA();

        public AccessorialChargesBLL()
        {
            this.pubParasInfo = null;
        }

        public AccessorialChargesBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public AccessorialChargesBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteAccessorialCharges(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                List<AccessorialChargesInfo> AccessorialChargesInfos = new List<AccessorialChargesInfo>();
                AccessorialChargesInfo contractInfo = null;
                foreach (string id in IDS)
                {
                    contractInfo = AccessorialChargesDA.GetAccessorialChargesByID(dataBase, tran, id, this.dataBaseID[0]);
                    AccessorialChargesInfos.Add(contractInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (AccessorialChargesDA.DeleteAccessorialCharges(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<AccessorialChargesInfo>(dataBase, tran, AccessorialChargesInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "ACCESSORIAL_CHARGES", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public AccessorialChargesOrderInfo GetAccessorialChargesByID(string orderID, string accessorialType)
        {
            AccessorialChargesOrderInfo accessorialChargesOrderInfo = new AccessorialChargesOrderInfo();
            string CS40001 = accessorialType;
            if (CS40001 != null)
            {
                if (!(CS40001 == "InBound"))
                {
                    if (CS40001 != "OutBound")
                    {
                        if (CS40001 == "Movement")
                        {
                            accessorialChargesOrderInfo = AccessorialChargesDA.GetAccessorialChargesMovementByID(orderID, this.dataBaseID[0], this.pubParasInfo);
                            if (accessorialChargesOrderInfo != null)
                            {
                                accessorialChargesOrderInfo.AccessorialChargesOrderDetailInfos = AccessorialChargesDA.GetAccessorialChargesMovementDetailByID(orderID, this.dataBaseID[0], this.pubParasInfo);
                                accessorialChargesOrderInfo.AccessorialChargesInfos = AccessorialChargesDA.GetAccessorialChargesByID(orderID, accessorialType, this.dataBaseID[0]);
                            }
                        }
                        return accessorialChargesOrderInfo;
                    }
                }
                else
                {
                    accessorialChargesOrderInfo = AccessorialChargesDA.GetAccessorialChargesInBoundOrderByID(orderID, this.dataBaseID[0], this.pubParasInfo);
                    if (accessorialChargesOrderInfo != null)
                    {
                        accessorialChargesOrderInfo.AccessorialChargesOrderDetailInfos = AccessorialChargesDA.GetAccessorialChargesInBoundOrderDetailByID(orderID, this.dataBaseID[0], this.pubParasInfo);
                        accessorialChargesOrderInfo.AccessorialChargesInfos = AccessorialChargesDA.GetAccessorialChargesByID(orderID, accessorialType, this.dataBaseID[0]);
                    }
                    return accessorialChargesOrderInfo;
                }
                accessorialChargesOrderInfo = AccessorialChargesDA.GetAccessorialChargesOutBoundOrderByID(orderID, this.dataBaseID[0], this.pubParasInfo);
                if (accessorialChargesOrderInfo != null)
                {
                    accessorialChargesOrderInfo.AccessorialChargesOrderDetailInfos = AccessorialChargesDA.GetAccessorialChargesOutBoundOrderDetailByID(orderID, this.dataBaseID[0], this.pubParasInfo);
                    accessorialChargesOrderInfo.AccessorialChargesInfos = AccessorialChargesDA.GetAccessorialChargesByID(orderID, accessorialType, this.dataBaseID[0]);
                }
            }
            return accessorialChargesOrderInfo;
        }

        public DataSet GetAccessorialChargesByQueryList(AccessorialChargesQueryEntity accessorialChargesQueryEntity)
        {
            List<string> sqlWhere = new List<string>();
            DataSet ds = null;
            string CS40001 = accessorialChargesQueryEntity.AccessorialType;
            if (CS40001 == null)
            {
                return ds;
            }
            if (!(CS40001 == "InBound"))
            {
                if (CS40001 != "OutBound")
                {
                    if (CS40001 == "Movement")
                    {
                        if (!string.IsNullOrEmpty(accessorialChargesQueryEntity.OrderID))
                        {
                            sqlWhere.Add(" R.MOVEMENT_ID " + accessorialChargesQueryEntity.OrderID);
                        }
                        if (!string.IsNullOrEmpty(accessorialChargesQueryEntity.OwnerID))
                        {
                            sqlWhere.Add(" R.OWNER_ID " + accessorialChargesQueryEntity.OwnerID);
                        }
                        if (!string.IsNullOrEmpty(accessorialChargesQueryEntity.CreatedBy))
                        {
                            sqlWhere.Add(" R.CREATED_BY " + accessorialChargesQueryEntity.CreatedBy);
                        }
                        if (!string.IsNullOrEmpty(accessorialChargesQueryEntity.CreatedDate))
                        {
                            sqlWhere.Add(accessorialChargesQueryEntity.CreatedDate.Replace("@CreatedDate@", "R.CREATED_DATE"));
                        }
                        return AccessorialChargesDA.GetAccessorialChargesMovementByQueryList(sqlWhere, accessorialChargesQueryEntity, this.dataBaseID[0], this.pubParasInfo);
                    }
                    if (CS40001 != "Vas")
                    {
                        return ds;
                    }
                    if (!string.IsNullOrEmpty(accessorialChargesQueryEntity.OrderID))
                    {
                        sqlWhere.Add(" R.ORDER_ID " + accessorialChargesQueryEntity.OrderID);
                    }
                    if (!string.IsNullOrEmpty(accessorialChargesQueryEntity.OwnerID))
                    {
                        sqlWhere.Add(" R.OWNER_ID " + accessorialChargesQueryEntity.OwnerID);
                    }
                    if (!string.IsNullOrEmpty(accessorialChargesQueryEntity.CreatedBy))
                    {
                        sqlWhere.Add(" R.CREATED_BY " + accessorialChargesQueryEntity.CreatedBy);
                    }
                    if (!string.IsNullOrEmpty(accessorialChargesQueryEntity.CreatedDate))
                    {
                        sqlWhere.Add(accessorialChargesQueryEntity.CreatedDate.Replace("@CreatedDate@", "R.CREATED_DATE"));
                    }
                    return null;
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(accessorialChargesQueryEntity.OrderID))
                {
                    sqlWhere.Add(" RECEIPT_ID " + accessorialChargesQueryEntity.OrderID);
                }
                if (!string.IsNullOrEmpty(accessorialChargesQueryEntity.OwnerID))
                {
                    sqlWhere.Add(" R.OWNER_ID " + accessorialChargesQueryEntity.OwnerID);
                }
                if (!string.IsNullOrEmpty(accessorialChargesQueryEntity.CreatedBy))
                {
                    sqlWhere.Add(" R.CREATED_BY " + accessorialChargesQueryEntity.CreatedBy);
                }
                if (!string.IsNullOrEmpty(accessorialChargesQueryEntity.CreatedDate))
                {
                    sqlWhere.Add(accessorialChargesQueryEntity.CreatedDate.Replace("@CreatedDate@", "R.CREATED_DATE"));
                }
                return AccessorialChargesDA.GetAccessorialChargesInBoundByQueryList(sqlWhere, accessorialChargesQueryEntity, this.dataBaseID[0], this.pubParasInfo);
            }
            if (!string.IsNullOrEmpty(accessorialChargesQueryEntity.OrderID))
            {
                sqlWhere.Add(" R.ORDER_ID " + accessorialChargesQueryEntity.OrderID);
            }
            if (!string.IsNullOrEmpty(accessorialChargesQueryEntity.OwnerID))
            {
                sqlWhere.Add(" R.OWNER_ID " + accessorialChargesQueryEntity.OwnerID);
            }
            if (!string.IsNullOrEmpty(accessorialChargesQueryEntity.CreatedBy))
            {
                sqlWhere.Add(" R.CREATED_BY " + accessorialChargesQueryEntity.CreatedBy);
            }
            if (!string.IsNullOrEmpty(accessorialChargesQueryEntity.CreatedDate))
            {
                sqlWhere.Add(accessorialChargesQueryEntity.CreatedDate.Replace("@CreatedDate@", "R.CREATED_DATE"));
            }
            return AccessorialChargesDA.GetAccessorialChargesOutBoundByQueryList(sqlWhere, accessorialChargesQueryEntity, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetAllAccessorialCharges()
        {
            return AccessorialChargesDA.GetAllAccessorialCharges(this.dataBaseID[0]);
        }

        public string InsertAccessorialCharges(AccessorialChargesInfo obj)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strAccessorialChargesID = string.Empty;
            string billingObjectName = string.Empty;
            bool billingObject = true;
            try
            {
                if (obj.BillingObject.ToUpper() == "Owner".ToUpper())
                {
                    TradePartnerInfo tradePartnerInfo = new TradePartnerInfo();
                    tradePartnerInfo = tradePartnerDA.GetTradePartnerByID(dataBase, tran, obj.BillingObjectID, whLoginID, this.pubParasInfo);
                    if (tradePartnerInfo != null)
                    {
                        billingObjectName = tradePartnerInfo.Company;
                    }
                    else
                    {
                        billingObject = false;
                    }
                }
                else if (obj.BillingObject.ToUpper() == "Vendor".ToUpper())
                {
                    VendorInfo vendorInfo = new VendorInfo();
                    vendorInfo = vendorDA.GetVendorByID(dataBase, tran, obj.BillingObjectID, whLoginID, this.pubParasInfo);
                    if (vendorInfo != null)
                    {
                        billingObjectName = vendorInfo.Company;
                    }
                    else
                    {
                        billingObject = false;
                    }
                }
                else if (obj.BillingObject.ToUpper() == "Customer".ToUpper())
                {
                    CustomerInfo customerInfo = new CustomerInfo();
                    customerInfo = customerDA.GetCustomerByID(dataBase, tran, obj.BillingObjectID, whLoginID, this.pubParasInfo);
                    if (customerInfo != null)
                    {
                        billingObjectName = customerInfo.Company;
                    }
                    else
                    {
                        billingObject = false;
                    }
                }
                else if (obj.BillingObject.ToUpper() == "Carrier".ToUpper())
                {
                    CarrierInfo carrierInfo = new CarrierInfo();
                    carrierInfo = carrierDA.GetCarrierByID(dataBase, tran, obj.BillingObjectID, whLoginID, this.pubParasInfo);
                    if (carrierInfo != null)
                    {
                        billingObjectName = carrierInfo.Company;
                    }
                    else
                    {
                        billingObject = false;
                    }
                }
                else if (obj.BillingObject.ToUpper() == "BillingEntity".ToUpper())
                {
                    BillingEntityInfo billingEntityInfo = new BillingEntityInfo();
                    billingEntityInfo = billingEntityDA.GetBillingEntityByID(dataBase, tran, obj.BillingObjectID, whLoginID, this.pubParasInfo);
                    if (billingEntityInfo != null)
                    {
                        billingObjectName = billingEntityInfo.Company;
                    }
                    else
                    {
                        billingObject = false;
                    }
                }
                if (!billingObject)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F9180_003", this.pubParasInfo.UserLanguage, new List<string> { obj.BillingObjectID });
                }
                if (string.IsNullOrEmpty(obj.AccessorialChargesID))
                {
                    obj.AccessorialChargesID = commonMethod.GenNextNumStr(dataBase, tran, "ACCESSORIAL_CHARGES", whLoginID);
                }
                obj.UpdatedBy = obj.CreatedBy;
                obj.CreatedDate = DateTime.Now;
                obj.UpdatedDate = DateTime.Now;
                if (AccessorialChargesDA.InsertAccessorialCharges(dataBase, tran, obj, whLoginID) > 0)
                {
                    BillingCredenceInfo billingCredenceInfo = new BillingCredenceInfo {
                        WhID = obj.WhID,
                        CredenceType = "AccessorialCharges",
                        DocID = obj.OrderID,
                        DocLineID = obj.OrderLineID,
                        DocType = obj.ChargeType,
                        OwnerID = obj.OwnerID,
                        SkuID = obj.SkuID,
                        TotalNetWeight = obj.TotalNetWeight,
                        TotalVolume = obj.TotalNetVolume,
                        BillingObject = obj.BillingObject,
                        BillingObjectID = obj.BillingObjectID,
                        BillingObjectName = billingObjectName,
                        CostType = obj.CostType,
                        DueFromTo = obj.DueFromTo,
                        ChargeUnit = obj.ChargeUnit,
                        FixedRate = decimal.Parse(obj.RateID),
                        Quantity = obj.BillingQty,
                        BillingAmount = obj.BillingAmount,
                        Remark = obj.Remark,
                        CreatedBy = obj.CreatedBy,
                        CreatedDate = obj.CreatedDate,
                        UpdatedBy = obj.UpdatedBy,
                        UpdatedDate = obj.UpdatedDate,
                        AccessorialChargesID = obj.AccessorialChargesID,
                        BillingCredenceID = commonMethod.GenNextNumStr(dataBase, tran, "BILLING_CREDENCE", whLoginID)
                    };
                    if (obj.ChargeType.ToUpper() == "InBound".ToUpper())
                    {
                        ReceiptInfo receiptInfo = new ReceiptInfo();
                        receiptInfo = receiptDA.GetReceiptByID(dataBase, tran, obj.OrderID, whLoginID, this.pubParasInfo);
                        if (receiptInfo != null)
                        {
                            billingCredenceInfo.ExternalDocID = receiptInfo.ExternalReceiptID;
                        }
                    }
                    else if (obj.ChargeType.ToUpper() == "OutBound".ToUpper())
                    {
                        OrdersInfo ordersInfo = new OrdersInfo();
                        ordersInfo = ordersDA.GetOrdersByID(dataBase, tran, obj.OrderID, whLoginID, this.pubParasInfo);
                        if (ordersInfo != null)
                        {
                            billingCredenceInfo.ExternalDocID = ordersInfo.ExternOrderID;
                        }
                    }
                    billingCredenceDA.InsertBillingCredence(dataBase, tran, billingCredenceInfo, this.dataBaseID);
                    strAccessorialChargesID = obj.AccessorialChargesID;
                    new AuditDataBLL().InsertAuditData<AccessorialChargesInfo>(dataBase, tran, null, obj, AuditAction.Insert, this.pubParasInfo.UserID, "ACCESSORIAL_CHARGES", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strAccessorialChargesID;
        }

        public string UpdateAccessorialCharges(AccessorialChargesInfo obj)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strAccessorialChargesID = string.Empty;
            try
            {
                AccessorialChargesInfo oldAccessorialChargesInfo = AccessorialChargesDA.GetAccessorialChargesByID(dataBase, tran, obj.AccessorialChargesID, this.dataBaseID[0]);
                if (AccessorialChargesDA.UpdateAccessorialCharges(dataBase, tran, obj, whLoginID) > 0)
                {
                    strAccessorialChargesID = obj.AccessorialChargesID;
                    new AuditDataBLL().InsertAuditData<AccessorialChargesInfo>(dataBase, tran, oldAccessorialChargesInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "ACCESSORIAL_CHARGES", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strAccessorialChargesID;
        }
    }
}

